package com.licode.codekit.db;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;

/**
 * @author licode
 */
@State(name = "CodeSettings", storages = {@Storage("licode.xml")})
public class Settings implements PersistentStateComponent<Settings> {
    private String dbUser;
    private String dbPwd;
    private String dbUri;
    private String dbDriver;

    public String getDbUser() {
        return dbUser;
    }

    public void setDbUser(String dbUser) {
        this.dbUser = dbUser;
    }

    public String getDbPwd() {
        return dbPwd;
    }

    public void setDbPwd(String dbPwd) {
        this.dbPwd = dbPwd;
    }

    public String getDbUri() {
        return dbUri;
    }

    public void setDbUri(String dbUri) {
        this.dbUri = dbUri;
    }

    public String getDbDriver() {
        return dbDriver;
    }

    public void setDbDriver(String dbDriver) {
        this.dbDriver = dbDriver;
    }

    public static Settings getInstance() {
        return ServiceManager.getService(Settings.class);
    }

    public Settings(){
        init();
    }

    private void init() {
        dbUser = "root";
        dbPwd = "123456";
        dbUri = "jdbc:mysql://localhost:3306/xxx?serverTimezone=Asia/Shanghai";
        dbDriver = "c:/data/mysql-driver.jar";
    }

    @Nullable
    @Override
    public Settings getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull Settings optionalSettings) {
        XmlSerializerUtil.copyBean(optionalSettings, this);
    }
}
